package com.driverstation;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.View;

public class ControllerView extends View
{
    Bitmap controllerBitmap;
    Bitmap greenSquare;
    Controller controller[] = new Controller[2];

    private Paint textPaint = new Paint();

    public ControllerView(Context context)
    {
        super(context);
        controllerBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.controller);
        controllerBitmap = getResizedBitmap(controllerBitmap, 800, 500);
        greenSquare = BitmapFactory.decodeResource(getResources(), R.drawable.small_green_square);
    }

    public void RegisterController(Controller controllerObject, int controllerNumber)
    {
        controller[controllerNumber] = controllerObject;
    }

    public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight)
    {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        //Resize
        matrix.postScale(scaleWidth, scaleHeight);
        //Return
        return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    }

    private void DisplayController(int controllerNumber, Canvas canvas)
    {
        int bitmapDistance = (controllerNumber == 1) ? 1120 : 0;
        //Display all buttons pressed, if any
        if(controller[controllerNumber].btnA)canvas.drawBitmap(greenSquare, 613 + bitmapDistance, 245, null);
        if(controller[controllerNumber].btnB)canvas.drawBitmap(greenSquare, 673 + bitmapDistance, 187, null);
        if(controller[controllerNumber].btnX)canvas.drawBitmap(greenSquare, 553 + bitmapDistance, 187, null);
        if(controller[controllerNumber].btnY)canvas.drawBitmap(greenSquare, 613 + bitmapDistance, 127, null);

        if(controller[controllerNumber].btnSelect)canvas.drawBitmap(greenSquare, 299 + bitmapDistance, 123, null);
        if(controller[controllerNumber].btnStart)canvas.drawBitmap(greenSquare, 467 + bitmapDistance, 123, null);

        if(controller[controllerNumber].btnLeftTrigger)canvas.drawBitmap(greenSquare, 120 + bitmapDistance, 0, null);
        if(controller[controllerNumber].btnRightTrigger)canvas.drawBitmap(greenSquare, 650 + bitmapDistance, 0, null);
        if(controller[controllerNumber].btnLeftBumper)canvas.drawBitmap(greenSquare, 170 + bitmapDistance, 0, null);
        if(controller[controllerNumber].btnRightBumper)canvas.drawBitmap(greenSquare, 600 + bitmapDistance, 0, null);

        if(controller[controllerNumber].leftStickClick)canvas.drawBitmap(greenSquare, 357 + bitmapDistance, 317, null);
        if(controller[controllerNumber].rightStickClick)canvas.drawBitmap(greenSquare, 407 + bitmapDistance, 317, null);

        //Display joystick positions
        canvas.drawBitmap(greenSquare, 265 + bitmapDistance + controller[controllerNumber].joyLeftX * 50, 317 + controller[controllerNumber].joyLeftY * 50, null);
        canvas.drawBitmap(greenSquare, 499 + bitmapDistance + controller[controllerNumber].joyRightX * 50, 317 + controller[controllerNumber].joyRightY * 50, null);

        //Display top hat
        if(controller[controllerNumber].topHatUp)canvas.drawBitmap(greenSquare, 151 + bitmapDistance, 133, null);
        if(controller[controllerNumber].topHatDown)canvas.drawBitmap(greenSquare, 151 + bitmapDistance, 229, null);
        if(controller[controllerNumber].topHatLeft)canvas.drawBitmap(greenSquare, 101 + bitmapDistance, 181, null);
        if(controller[controllerNumber].topHatRight)canvas.drawBitmap(greenSquare, 201 + bitmapDistance, 181, null);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        textPaint.setTextSize(40);

        if (controller[0].assignmentNeeded)
        {
            canvas.drawText("Press a button on controller 1", 10, 50, textPaint);
            return;
        }
        if (controller[1].assignmentNeeded)
        {
            canvas.drawText("Press a button on controller 2", 10, 50, textPaint);
            return;
        }

        canvas.drawBitmap(controllerBitmap, 0, 0, null);
        canvas.drawBitmap(controllerBitmap, canvas.getWidth() - controllerBitmap.getWidth(), 0, null);

        DisplayController(0, canvas);
        DisplayController(1, canvas);
    }
}
